package com.mstanford.movie.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public final class DataUtil {

	private DataUtil() {}
	
	/**
	 * 判断是否为空，true空，false非空
	 * @param obj
	 * @return
	 */
	public final static boolean isEmpty(Object obj) {
		
		if(obj instanceof String)
			return obj == null || ((String)obj).trim().isEmpty();
		else if(obj instanceof Map<?, ?>)
			return obj == null || ((Map<?, ?>)obj).isEmpty();
		else if(obj instanceof List<?>)
			return obj == null || ((List<?>)obj).isEmpty();
		else
			return obj == null || obj.toString().trim().isEmpty();
	}
	
	/**
	 * 将Map转换为Json格式
	 * @param map
	 * @return
	 */
	public final static String mapToJson(Map<String, String> map) {
		
		String json = null;
		StringBuffer sb = new StringBuffer();
		
		for(String key : map.keySet()) {
			String value = map.get(key);
			
			sb.append("\"").append(key).append("\":\"").append(value).append("\",");
		}
		
		int lastIndex = sb.lastIndexOf(",");
		
		if(lastIndex != -1) {
			json = "{" + sb.substring(0, lastIndex) + "}";
		}
		
		return json;
	}
	
	/**
	 * 将Map转换为Json格式
	 * @param map
	 * @return
	 */
	public final static String map2Json(Map<String, List<String>> map) {
		
		String json = null;
		StringBuffer sb = new StringBuffer();
		
		for(String key : map.keySet()) {
			List<String> list = map.get(key);
			
			sb.append("\"").append(key).append("\":").append(list2Json(list)).append(",");
		}
		
		int lastIndex = sb.lastIndexOf(",");
		
		if(lastIndex != -1) {
			json = "{" + sb.substring(0, lastIndex) + "}";
		}
		
		return json;
	}
	
	/**
	 * 将List转换为Json格式
	 * @param list
	 * @return
	 */
	public final static String listToJson(List<Map<String, String>> list) {
		
		String json = null;
		StringBuffer sb = new StringBuffer();
		
		for(Map<String, String> map : list) {
			sb.append(mapToJson(map)).append(",");
		}
		
		int lastIndex = sb.lastIndexOf(",");
		
		if(lastIndex != -1) {
			json = "[" + sb.substring(0, lastIndex) + "]";
		}
		
		return json;
	}
	
	/**
	 * 将List转换为Json格式
	 * @param list
	 * @return
	 */
	public final static String list2Json(List<String> list) {
		
		String json = null;
		StringBuffer sb = new StringBuffer();
		
		for(String val : list) {
			sb.append("\"").append(val).append("\",");
		}
		
		int lastIndex = sb.lastIndexOf(",");
		
		if(lastIndex != -1) {
			json = "[" + sb.substring(0, lastIndex) + "]";
		}
		
		return json;
	}
	
	/**
	 * 比较日期大小,date1等于date2返回0,date1小于date2返回-1,date1大于date2返回1
	 * @param date1
	 * @param date2
	 * @param pattern
	 * @return
	 */
	public static int compareDate(String date1, String date2, String pattern) {
		
		DateFormat df = new SimpleDateFormat(pattern);
		
		try {
			Date d1 = df.parse(date1);
			Date d2 = df.parse(date2);

			return d1.compareTo(d2);
		} catch (ParseException e) {
			return -2;
		}
	}
	
	/**
	 * 构建分页数据
	 * @param map
	 */
	public final static void buildPagingMap(Map<String, String> map) {
		
		String pageId = map.get("pageId");
		String pageSize = map.get("pageSize");
		String dataCount = map.get("dataCount");
		
		if(DataUtil.isEmpty(pageId) || Integer.parseInt(pageId) <= 0) {
			map.put("pageId", ConstantUtil.PAGE_ID + "");
		}

		if(DataUtil.isEmpty(pageSize) || Integer.parseInt(pageSize) <= 0) {
			map.put("pageSize", ConstantUtil.PAGE_SIZE + "");
		}
		
		pageId = map.get("pageId");
		pageSize = map.get("pageSize");
		
		int pos = (Integer.parseInt(pageId) - ConstantUtil.PAGE_ID) * Integer.parseInt(pageSize);
		if(pos <= 0) {
			pos = 0;
		}

		int limit = pos + Integer.parseInt(pageSize);
		
		int pageCount = Integer.parseInt(dataCount) / Integer.parseInt(pageSize);
		int mod = Integer.parseInt(dataCount) % Integer.parseInt(pageSize);
		
		if(mod != 0) {
			pageCount ++;
		}
		
		map.put("pos", pos + "");
		map.put("limit", limit + "");
		map.put("pageCount", pageCount + "");
	}
	
	/**
	 * 获取分页SQL，SQLServer2005及以上版本
	 * @param sql
	 * @param limit 提取的记录
	 * @param pos 过滤的记录
	 * @return
	 */
	public final static String getPagingSqlBySQLServer2005High(String sql, int limit, int pos) {
		
		sql = sql.trim();
		int selectIndex = Math.max(sql.indexOf("select"), sql.indexOf("SELECT"));
		String selectOther = sql.substring(selectIndex + 6);
		
		StringBuffer pagingSql = new StringBuffer(sql.length() + 100);
		pagingSql.append("select * from (select *,row_number() over(order by rownum) rownum_ ")
				 .append("from (select top ").append(limit).append(" 0 rownum,").append(selectOther)
				 .append(") tab_) tab_out_ where rownum_>").append(pos);
		
		return pagingSql.toString();
	}
	
	public final static String createUUID() {
		
		return UUID.randomUUID().toString();
	}
	
	public final static Date string2Date(String str, String pattern) {
		
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		
		try {
			return sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public final static String date2String(Date date, String pattern) {
		
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		
		return sdf.format(date);
	}
}